package com.hpeng.daily.action;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.gl.service.DepartmentService;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.model.Category;
import com.hpeng.sys.model.Department;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardAction")
@Scope("prototype")
public class AssetCardAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	// 用于分页显示
	private int page;
	public String pageLink;
	private Pagination<AssetCard> pagination;
	
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private LoginUserMgr loginUser;
	@Resource
	private DepartmentService departmentService;
	private List<Department> departments;
	private AssetCard assetCard;
	private String idArray; // 获取被选择的Id编号数组 从页面上传到后台的数组
	private String msg;
	private String catid; // 类别编号
	private String acid; // 资产的编号
	private String result;// 处理意见
	private List<Lookup> ls;// 用于页面上显示资产的状态的list
	// 用于根据权限生成功能按钮
	@Resource
	private Function function;
	private String accessRight;
	private String FN;
	// 条件查询
	private String condition; // 查询条件
	private String conditionValue;// 输入的查询值
	private String acstatusValue;

	
	// 通用删除方法,可根据单个条件删除,可根据数组删除.
	public String deleteAllAssetCard() {
		try {
			String[] stringArray = idArray.split(",");
			for (int i = 0; i < stringArray.length; i++) {
				AssetCard ac = assetCardManager.findAssetCard(stringArray[i]);
				if(ac.getAcstatus().equals("110")){
				assetCardManager.deleteAssetCard(ac, "D", loginUser
						.getUserId(request()),function.getStatusDesc("1", "10"));
				}else{
					msg = getText("只能删除申报-经办已录入的资产");
				}
			}
		} catch (RuntimeException rx) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		return searchAssetCard();
	}

	// 按照条件进行查询
	public String queryAssetCard() {
		try {
			if(conditionValue!=null){
			       conditionValue = new String(conditionValue.getBytes("ISO8859-1"),
					"UTF-8");
			       }
			String append = "";
			if ("zclb".equals(condition)) {
				append = " and a.category.catname like ('%"+conditionValue+"%') ";
			} else if ("zczt".equals(condition)) {
				append = " and acstatus like ('%"+acstatusValue+"%') ";
			} else if ("sbbm".equals(condition)) {
				append = " and reportdepartment like ('%"+conditionValue+"%') ";
			}
			String[] hql = {
					" from AssetCard a where a.acstatus like '"
					+ 1 + "%' ",
					append };
			String order = " order by a.acid asc";
			setPagination(assetCardManager.getPagination(page, hql, order));
			pageLink = pagination
					.getPageLink("queryAssetCard.action?condition=" + condition
							+ "&conditionValue=" + conditionValue+"&acstatusValue=" + acstatusValue);
			this.accessRight = function.getAccessRight(session(), FN);
			ls = function.getLookup("prod_status_mst", "status_code||sub_code",
					"status_desc||' - '||sub_desc");
		} catch (UnsupportedEncodingException e) {
			System.out.println(e.getMessage());
		}
		return "success";
	}

	// 用于分页查询 显示资产申报
	public String searchAssetCard() {
		String tName = loginUser.getUserTemplateName(request());
		String order = " ";
		if (tName.contains("管理员")) {
			String[] hql = { " from AssetCard a where a.acstatus like '1%'" };
			setPagination(assetCardManager.getPagination(page, hql, order));
		}
		if (tName.contains("部门经办")) {
			String[] hql = { " from AssetCard a where a.acstatus like '110' or a.acstatus like '111'" };
			setPagination(assetCardManager.getPagination(page, hql, order));
		}
		if (tName.contains("部门领导")) {
			String[] hql = { " from AssetCard a where a.acstatus like '120' or a.acstatus like '121'" };
			setPagination(assetCardManager.getPagination(page, hql, order));
		}
		if (tName.contains("局领导")) {
			String[] hql = { " from AssetCard a where a.acstatus like '121'" };
			setPagination(assetCardManager.getPagination(page, hql, order));
		}
		pageLink = pagination.getPageLink("searchAssetCard.action?FN=" + FN);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}

	// 点击类别树上的类别得到该类别下的申报资产
	public String queryAssetByCategory() {
		String[] hql = { " from AssetCard where catid like '"+catid+"%' and acstatus like '1%' " };
		String order = " ";
		setPagination(assetCardManager.getPagination(page, hql, order));
		pageLink = pagination.getPageLink("queryAssetByCategory.action?FN=" + FN+"&catid="+catid);
		this.accessRight = function.getAccessRight(session(), FN);
		ls = function.getLookup("prod_status_mst", "status_code||sub_code",
				"status_desc||' - '||sub_desc");
		return "success";
	}

	// 用于录入申报资产信息
	public String addReport() {
		try {
			Category c = new Category();
			c.setCatid(catid);
			assetCard.setCategory(c);
			// 生成相同类型的多张资产卡片 编号不相同
			// 此时申请的资产状态为：申报-部门经办录入
			assetCard.setAcstatus("110");
			assetCardManager.addAssetCard(assetCard, "A", loginUser
					.getUserId(request()),function.getStatusDesc("1", "10"));
			this.accessRight = function.getAccessRight(session(), FN);
		} catch (RuntimeException e) {
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return searchAssetCard();
	}

	// 查询某条申报资产记录
	public String findCard() {
		assetCard = assetCardManager.findAssetCard(acid);
		departments = departmentService.findDepartment();
		return "success";
	}

	// 当状态为申报-部门经办录入进行资产修改
	public String updateAssetCard() {
		try {
			Category c = new Category();
			c.setCatid(catid);
			assetCard.setCategory(c);
			System.out.println("你现在修改的申报资产的状态为:"+assetCard.getAcstatus());
			if(assetCard.getAcstatus().equals("110")){
			   assetCard.setAcstatus("110");
			   assetCardManager.updateAssetCard(assetCard, "E", loginUser
					.getUserId(request()), null,function.getStatusDesc("1", "10"));
			}else{
				msg = getText("只能修改状态为申报--经办已录入的资产");
			}
		} catch (RuntimeException rx) {
			rx.printStackTrace();
			msg = getText("update.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("update.success");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		return searchAssetCard();
	}

	//上报资产// 实际上就是修改资产状态
	public String reportAssetCard() {
		String[] stringArray = idArray.split(",");
		String tName = loginUser.getUserTemplateName(request());
		if (tName.contains("管理员")||tName.contains("部门经办")||tName.contains("部门领导")||tName.contains("局领导")) {
			function.updStatus("1", "11", stringArray);
		}
		this.accessRight = function.getAccessRight(session(), FN);
		return searchAssetCard();
	}

	// 审核资产
	public String examineAssetCard() {
		try{
		String[] stringArray = idArray.split(",");
		String tName = loginUser.getUserTemplateName(request());
		// 审核资产----部门领导已审核
		if (tName.contains("部门领导")||tName.contains("超级管理员")) {
			
			function.updStatus("1", "20", stringArray);
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("1", "20"));
		
			msg=getText("审核成功");
			}
		}
		//审核资产--局领导已审核
		if (tName.contains("局领导")||tName.contains("超级管理员")) {
			function.updStatus("1", "31", stringArray);
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("1", "31"));
			}
			msg=getText("审核成功");
		}else{
			msg=getText("对不起，你没有审核的权限");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		} catch (Exception rx) {
			msg = getText("审核失败，请重新操作");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("审核成功");
		}
		return searchAssetCard();
	}

	// 退回资产
	public String returnedAssetCard() throws UnsupportedEncodingException {
		String[] stringArray = idArray.split(",");
		String tName = loginUser.getUserTemplateName(request());
		System.out.println("从页面上传到action中的处理意见是：" + result);
		// 部门领导退回到经办已录入
		if (tName.contains("部门领导")||tName.contains("超级管理员")) {
			function.updStatus("1", "10", stringArray);
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("1", "10"));
			}
			msg=getText("退回成功");
		}
		// 局领导退回到部门领导已审核
		if (tName.contains("局领导")||tName.contains("超级管理员")) {
			function.updStatus("1", "20", stringArray);
			for (String id : stringArray) {
				assetCard = assetCardManager.findAssetCard(id);
				assetCardManager.updateAssetCard(assetCard, "E", loginUser
						.getUserId(request()), result,function.getStatusDesc("1", "20"));
			}
			msg=getText("退回成功");
		}else{
			msg=getText("对不起，你没有退回的权限");
		}
		this.accessRight = function.getAccessRight(session(), FN);
		return searchAssetCard();
	}

	
	
	/********************** set get方法 ***********************************/
	public void setAssetCardManager(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public AssetCard getAssetCard() {
		return assetCard;
	}

	public void setAssetCard(AssetCard assetCard) {
		this.assetCard = assetCard;
	}

	public Pagination<AssetCard> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<AssetCard> pagination) {
		this.pagination = pagination;
	}

	public void setAssetCardManage(AssetCardManager assetCardManager) {
		this.assetCardManager = assetCardManager;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setIdArray(String idArray) {
		this.idArray = idArray;
	}

	public String getIdArray() {
		return idArray;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getCatid() {
		return catid;
	}

	public void setCatid(String catid) {
		this.catid = catid;
	}

	public String getAccessRight() {
		return accessRight;
	}

	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}

	public String getFN() {
		return FN;
	}

	public void setFN(String fN) {
		FN = fN;
	}

	public HttpServletRequest request() {
		return ServletActionContext.getRequest();
	}

	public Map<String, Object> session() {
		return ActionContext.getContext().getSession();
	}

	public String getAcid() {
		return acid;
	}

	public void setAcid(String acid) {
		this.acid = acid;
	}

	public List<Lookup> getLs() {
		return ls;
	}

	public void setLs(List<Lookup> ls) {
		this.ls = ls;
	}

	public List<Department> getDepartments() {
		return departments;
	}

	public void setDepartments(List<Department> departments) {
		this.departments = departments;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getConditionValue() {
		return conditionValue;
	}

	public void setConditionValue(String conditionValue) {
		this.conditionValue = conditionValue;
	}
	public String getAcstatusValue() {
		return acstatusValue;
	}

	public void setAcstatusValue(String acstatusValue) {
		this.acstatusValue = acstatusValue;
	}

}
